using UnityEditor;
using UnityEngine;

namespace IQIGame.Onigao.GamePlay.Editors
{
    public static class LevelGenLiftCollider
    {
        [MenuItem("GameObject/LevelPlay/生成电梯的碰撞体")]
        static public void MenuCreateLiftCollider(MenuCommand menuCommand)
        {
            var GameObject = Selection.activeGameObject;
            if (GameObject == null)
            {
                // 弹框提示不能为null
                EditorUtility.DisplayDialog("提示", "选中的实体GameObject不能为null", "OK");
                return;
            }
            LevelLiftInputDialog.ShowWindow();
        }

        static public void CreateLiftCollider(float floorHeight, float floorSizeX, float floorSizeZ, float doorWidth, float doorLength)
        {
            var go = Selection.activeGameObject;
            if (go == null)
            {
                return;
            }

            const float WallWidth = 0.2f;

            var colliderTransform = go.transform.Find("collider");
            if (colliderTransform != null)
            {
                Object.DestroyImmediate(colliderTransform.gameObject);
            }

            var collider = new GameObject("collider");
            collider.transform.parent = go.transform;
            collider.transform.localPosition = Vector3.zero;
            collider.transform.localRotation = Quaternion.identity;
            collider.transform.localScale = Vector3.one;

            var floor = new GameObject("floor");
            floor.transform.parent = collider.transform;
            var floorCollider = floor.AddComponent<BoxCollider>();
            floorCollider.center = new Vector3(0, floorHeight + 0.5f, 0);
            floorCollider.size = new Vector3(floorSizeX, 1, floorSizeZ);

            var closeWall = new GameObject("close_wall");
            closeWall.transform.parent = collider.transform;
            closeWall.SetActive(false);

            var closeWall1 = closeWall.AddComponent<BoxCollider>();
            closeWall1.center = new Vector3(floorSizeX / 2 + WallWidth / 2, floorHeight + 1, 0);
            closeWall1.size = new Vector3(WallWidth, 2, floorSizeZ + WallWidth * 2);

            var closeWall2 = closeWall.AddComponent<BoxCollider>();
            closeWall2.center = new Vector3(-floorSizeX / 2 - WallWidth / 2, floorHeight + 1, 0);
            closeWall2.size = new Vector3(WallWidth, 2, floorSizeZ + WallWidth * 2);

            var closeWall3 = closeWall.AddComponent<BoxCollider>();
            closeWall3.center = new Vector3(0, floorHeight + 1, floorSizeZ / 2 + WallWidth / 2);
            closeWall3.size = new Vector3(floorSizeX + WallWidth * 2, 2, WallWidth);

            var closeWall4 = closeWall.AddComponent<BoxCollider>();
            closeWall4.center = new Vector3(0, floorHeight + 1, -floorSizeZ / 2 - WallWidth / 2);
            closeWall4.size = new Vector3(floorSizeX + WallWidth * 2, 2, WallWidth);

            var entry = new GameObject("entry");
            entry.transform.parent = collider.transform;

            var entry1 = entry.AddComponent<BoxCollider>();
            entry1.center = new Vector3(0, floorHeight + 0.5f, -(doorLength / 2 + floorSizeZ / 2));
            entry1.size = new Vector3(doorWidth, 1, doorLength);

            var entry2 = entry.AddComponent<BoxCollider>();
            entry2.center = new Vector3(doorWidth / 2 + WallWidth / 2, floorHeight + 1, -(doorLength / 2 + floorSizeZ / 2));
            entry2.size = new Vector3(WallWidth, 2, doorLength);

            var entry3 = entry.AddComponent<BoxCollider>();
            entry3.center = new Vector3(-doorWidth / 2 - WallWidth / 2, floorHeight + 1, -(doorLength / 2 + floorSizeZ / 2));
            entry3.size = new Vector3(WallWidth, 2, doorLength);

            var exit = new GameObject("exit");
            exit.transform.parent = collider.transform;

            var exit1 = exit.AddComponent<BoxCollider>();
            exit1.center = new Vector3(0, floorHeight + 0.5f, doorLength / 2 + floorSizeZ / 2);
            exit1.size = new Vector3(doorWidth, 1, doorLength);

            var exit2 = exit.AddComponent<BoxCollider>();
            exit2.center = new Vector3(doorWidth / 2 + WallWidth / 2, floorHeight + 1, doorLength / 2 + floorSizeZ / 2);
            exit2.size = new Vector3(WallWidth, 2, doorLength);

            var exit3 = exit.AddComponent<BoxCollider>();
            exit3.center = new Vector3(-doorWidth / 2 - WallWidth / 2, floorHeight + 1, doorLength / 2 + floorSizeZ / 2);
            exit3.size = new Vector3(WallWidth, 2, doorLength);



            var openWall = new GameObject("open_wall");
            openWall.transform.parent = collider.transform;

            var openWall1 = openWall.AddComponent<BoxCollider>();
            openWall1.center = new Vector3(floorSizeX / 2 + WallWidth / 2, floorHeight + 1, 0);
            openWall1.size = new Vector3(WallWidth, 2, floorSizeZ + WallWidth * 2);

            var openWall2 = openWall.AddComponent<BoxCollider>();
            openWall2.center = new Vector3(-floorSizeX / 2 - WallWidth / 2, floorHeight + 1, 0);
            openWall2.size = new Vector3(WallWidth, 2, floorSizeZ + WallWidth * 2);


            // 根据上述代码，添加开放门的两边的碰撞墙
            var openWallSize = (floorSizeX - doorWidth) / 2;
            var XOffset = openWallSize / 2 + doorWidth / 2;
            var openWall31 = openWall.AddComponent<BoxCollider>();
            openWall31.center = new Vector3(XOffset, floorHeight + 1, floorSizeZ / 2 + WallWidth / 2);
            openWall31.size = new Vector3(openWallSize, 2, WallWidth);

            var openWall32 = openWall.AddComponent<BoxCollider>();
            openWall32.center = new Vector3(-XOffset, floorHeight + 1, floorSizeZ / 2 + WallWidth / 2);
            openWall32.size = new Vector3(openWallSize, 2, WallWidth);

            var openWall41 = openWall.AddComponent<BoxCollider>();
            openWall41.center = new Vector3(XOffset, floorHeight + 1, -floorSizeZ / 2 - WallWidth / 2);
            openWall41.size = new Vector3(openWallSize, 2, WallWidth);

            var openWall42 = openWall.AddComponent<BoxCollider>();
            openWall42.center = new Vector3(-XOffset, floorHeight + 1, -floorSizeZ / 2 - WallWidth / 2);
            openWall42.size = new Vector3(openWallSize, 2, WallWidth);

        }
    }


    public class LevelLiftInputDialog : EditorWindow
    {
        public float floorHeight;
        public float floorSizeX;
        public float floorSizeZ;
        public float doorWidth;
        public float doorLength;

        public void Read()
        {
            //尝试从编辑器持久化数据中读取上述变量
            floorHeight = EditorPrefs.GetFloat("[LevelLiftInputDialog|floorHeight]", 0);
            floorSizeX = EditorPrefs.GetFloat("[LevelLiftInputDialog|floorSizeX]", 0);
            floorSizeZ = EditorPrefs.GetFloat("[LevelLiftInputDialog|floorSizeZ]", 0);
            doorWidth = EditorPrefs.GetFloat("[LevelLiftInputDialog|doorWidth]", 0);
            doorLength = EditorPrefs.GetFloat("[LevelLiftInputDialog|doorLength]", 0);

        }

        //写个函数保存这些变量到编辑器持久化中
        private void Save()
        {
            EditorPrefs.SetFloat("[LevelLiftInputDialog|floorHeight]", floorHeight);
            EditorPrefs.SetFloat("[LevelLiftInputDialog|floorSizeX]", floorSizeX);
            EditorPrefs.SetFloat("[LevelLiftInputDialog|floorSizeZ]", floorSizeZ);
            EditorPrefs.SetFloat("[LevelLiftInputDialog|doorWidth]", doorWidth);
            EditorPrefs.SetFloat("[LevelLiftInputDialog|doorLength]", doorLength);
        }

        public static LevelLiftInputDialog ShowWindow()
        {
            return EditorWindow.GetWindow(typeof(LevelLiftInputDialog)) as LevelLiftInputDialog;
        }

        private void Awake()
        {
            Read();
        }

        private void OnGUI()
        {
            EditorGUILayout.LabelField("电梯碰撞体创建");
            EditorGUILayout.BeginHorizontal();
            //输入height
            EditorGUILayout.LabelField("Floor Height:");
            floorHeight = EditorGUILayout.FloatField(floorHeight);
            EditorGUILayout.EndHorizontal();
            // 依照上面，使用布局，输入剩余的变量值
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("Floor Size X:");
            floorSizeX = EditorGUILayout.FloatField(floorSizeX);
            EditorGUILayout.EndHorizontal();
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("Floor Size Z:");
            floorSizeZ = EditorGUILayout.FloatField(floorSizeZ);
            EditorGUILayout.EndHorizontal();
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("Door Width:");
            doorWidth = EditorGUILayout.FloatField(doorWidth);
            EditorGUILayout.EndHorizontal();
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("Door Length:");
            doorLength = EditorGUILayout.FloatField(doorLength);
            EditorGUILayout.EndHorizontal();

            if (GUILayout.Button("OK"))
            {
                Save();
                // 调用 LevelGenLiftCollider.CreateLiftCollider
                LevelGenLiftCollider.CreateLiftCollider(floorHeight, floorSizeX, floorSizeZ, doorWidth, doorLength);

                this.Close();
            }
        }
    }

}